/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dede.screensaver;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

/**
 * Settings for Clock Daydream
 */
public final class ScreensaverSettingsActivity extends AppCompatActivity {

    public static final String KEY_CLOCK_STYLE = "screensaver_clock_style";
    public static final String KEY_NIGHT_MODE = "screensaver_night_mode";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screensaver_settings);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    public static class PrefsFragment extends PreferenceFragment
            implements Preference.OnPreferenceChangeListener {

        public final static String BACK_DISABLE = "back_disable";
        public final static String DOUBLE_PRESS_ENABLE = "double_press_enable";
        public final static String DOUBLE_PRESS_DELAY = "double_press_delay";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.dream_settings);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ListView listView = view.findViewById(android.R.id.list);
            listView.setDividerHeight(Utils.dp2px(getActivity(), .5f));
        }

        @Override
        public void onResume() {
            super.onResume();
            refresh();
        }

        @Override
        public boolean onPreferenceChange(Preference pref, Object newValue) {
            if (KEY_CLOCK_STYLE.equals(pref.getKey())) {
                final ListPreference clockStylePref = (ListPreference) pref;
                final int index = clockStylePref.findIndexOfValue((String) newValue);
                clockStylePref.setSummary(clockStylePref.getEntries()[index]);
            }
            switch (pref.getKey()) {
                case BACK_DISABLE:
                    break;
                case DOUBLE_PRESS_ENABLE:
                    if (!(boolean) newValue) {
                        getPreferenceScreen().removePreference(doublePressDelay);
                    } else {
                        getPreferenceScreen().addPreference(doublePressDelay);
                    }
                    break;
                case DOUBLE_PRESS_DELAY:
                    doublePressDelay.setSummary(newValue + "ms");
                    break;
                default:
                    return false;
            }
            return true;
        }

        EditTextPreference doublePressDelay;

        private void refresh() {
            final ListPreference clockStylePref = (ListPreference) findPreference(KEY_CLOCK_STYLE);
            clockStylePref.setSummary(clockStylePref.getEntry());
            clockStylePref.setOnPreferenceChangeListener(this);

            CheckBoxPreference backEnable = (CheckBoxPreference) findPreference(BACK_DISABLE);
            backEnable.setOnPreferenceChangeListener(this);

            CheckBoxPreference doublePressEnable = (CheckBoxPreference) findPreference(DOUBLE_PRESS_ENABLE);
            doublePressEnable.setOnPreferenceChangeListener(this);

            doublePressDelay = (EditTextPreference) findPreference(DOUBLE_PRESS_DELAY);
            doublePressDelay.setSummary(doublePressDelay.getText() + "ms");
            doublePressDelay.setOnPreferenceChangeListener(this);
            doublePressEnable.setOnPreferenceChangeListener(this);
            if (!doublePressEnable.isChecked()) {
                getPreferenceScreen().removePreference(doublePressDelay);
            }
        }
    }
}
